/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.siscoh.controller;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.siscoh.model.bean.CategoriaBean;
import org.siscoh.model.bean.EspecificacaoBean;
import org.siscoh.model.dao.CategoriaDao;
import org.siscoh.model.dao.EspecificacaoDao;

/**
 *
 * @author Israel
 */
public class ControladorCategoria extends AbstractController {

    @Override
    public String executa(HttpServletRequest request, HttpServletResponse response) {
        String url = "404.html";
        int acao;
        
        acao = Integer.parseInt(request.getParameter("cod"));

        switch (acao) {
            case Constants.INSERT_CATEGORIA:
                url = novaCategoria(request, response);
                break;
        }
        

        return url;
    }

    private String novaCategoria(HttpServletRequest request, HttpServletResponse response) {
        String espec, url = "formularios/formcategoria";
        int i, status = 1;
        String nomeCat = request.getParameter("nomecategoria");

        if(nomeCat == null || nomeCat.equals(""))
        {
            url = url + "?status=" + status;
            return url;
        }
        
        CategoriaBean cb = new CategoriaBean();
        cb.setNome(nomeCat);

        i = 0;
        while (true) {
            espec = request.getParameter("especificacao" + i);

            if (espec == null) {
                break;
            }

            EspecificacaoBean eb = new EspecificacaoBean();
            eb.setChave(espec);
            cb.addEspecificacao(eb);
            i++;
        }

        new CategoriaDao().insert(cb);

        url = url + "?status=" + status;

        return url;
    }
}